package com.cscp.javastudy.juc.forkjoin;

import java.util.concurrent.RecursiveTask;

/**
 * 求和计算
 * ForkJoin的使用：
 *   1- forkJoinPool
 *   2- forkJoinPool.execute(...)
 */
public class ForkJoinDemo extends RecursiveTask<Long> {

    private Long start;//1
    private Long end;//1990900000
    private Long temp = 10000L;

    public ForkJoinDemo(Long start, Long end) {
        this.start = start;
        this.end = end;
    }


    @Override
    protected Long compute() {
        if ((end-start)<temp){
            Long sum = 0L;
            for (Long i = start; i < end; i++) {
                sum += i;
            }
            return sum;
        }else {//forkJoin
            long middle = (start+end) / 2;//中间值
            ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
            task1.fork();//拆分任务，把任务压入线程队列
            ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end);
            task2.fork();//拆分任务，把任务压入线程队列

            return task1.join()+task2.join();
        }
    }

}
